package com.sgb.kjwl.viewmodel.packers;


import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.SelfEmploymentEditEntity;
import com.sgb.kjwl.view.ui.activity.packers.EmploymentDetailActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.im.immodel.IMUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author cheng2020aly
 * @date 2020/11/11
 * description:临时用工详情的vieModel
 */
public class EmploymentDetailModel extends BaseViewModel {

    private EmploymentDetailActivity activity;

    public EmploymentDetailModel(EmploymentDetailActivity activity) {
        this.activity = activity;
    }

    public void getH5Url(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("homeworkforce").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no;
                    MLog.d("api", "H5跳转链接  " + url);
                    activity.getUrl(url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getShareH5Url(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("workforceshare").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no;
                    detailEmploymentShare(no, url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void detailEmployment(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().detailEmployment(no).enqueue(new Callback<BaseEntity<SelfEmploymentEditEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SelfEmploymentEditEntity>> call, Response<BaseEntity<SelfEmploymentEditEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.detailEmployment(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SelfEmploymentEditEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void detailEmploymentShare(String no, String url) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().detailEmployment(no).enqueue(new Callback<BaseEntity<SelfEmploymentEditEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SelfEmploymentEditEntity>> call, Response<BaseEntity<SelfEmploymentEditEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.detailEmploymentShare(response.body().getData(), url);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SelfEmploymentEditEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void detailEmploymentIm(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().detailEmployment(no).enqueue(new Callback<BaseEntity<SelfEmploymentEditEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SelfEmploymentEditEntity>> call, Response<BaseEntity<SelfEmploymentEditEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.detailEmploymentIm(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SelfEmploymentEditEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void findCustomerServiceImAccount(String imId) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("customerServiceContactsId", imId);
        APIManager.getInstance().getPickAPI().getfindCustomerServiceImAccount(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    activity.onBackAccount(response.body().getMsg());
                } else {
                    IMUtils.getInstance().buildSingleTalking(response.body().getData().toString());
                    // 接口返回的是String类型
                    String dataImId = response.body().getData().toString();
                    IMUtils.getInstance().buildSingleTalking(dataImId);
//                    NimUIKit.startP2PSession(activity, dataImId);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void detailEmploymentApply(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().detailEmployment(no).enqueue(new Callback<BaseEntity<SelfEmploymentEditEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SelfEmploymentEditEntity>> call, Response<BaseEntity<SelfEmploymentEditEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.detailEmploymentApply(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SelfEmploymentEditEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void followForTeam(String no, String type, boolean isAttention) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().follow(isAttention,no, Integer.parseInt(type)).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                if (data.isSuccess()){
                    activity.follow();
                }
            }
        });
    }
}
